package com.service.seedoctor.impl;

import com.comm.R;
import com.entity.*;
import com.github.pagehelper.PageHelper;
import com.mapper.seedoctor.NewVisitsMapper;
import com.service.seedoctor.NewVisitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewVisitsServiceImpl implements NewVisitsService {

    @Autowired
    private NewVisitsMapper newVisitsMapper;

    @Override
    public R selectRegisteredListByName(String bankName, Integer currentPage, Integer pageSize) {
        R r=new R();
        try {
            com.github.pagehelper.Page<Registered> page = PageHelper.startPage(currentPage,pageSize);
            //对紧跟在PageHelper使用后面的那条sql做分页处理
            List<Registered> registereds = newVisitsMapper.selectAllRegisterByName(bankName);
            System.out.println(registereds.size());
            long count =  page.getTotal();
            System.out.println("总记录数:"+count);
            r.setCode(200);
            r.setData(registereds);
            r.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R selectPatientBankByName(String bankName, Integer currentPage, Integer pageSize) {
        R r=new R();
        try {
            com.github.pagehelper.Page<PatientBank> page = PageHelper.startPage(currentPage,pageSize);
            //对紧跟在PageHelper使用后面的那条sql做分页处理
            List<PatientBank> patientBanks = newVisitsMapper.selectPatientBankByName(bankName);
            System.out.println(patientBanks.size());
             long count =  page.getTotal();
             System.out.println("总记录数:"+count);
            r.setCode(200);
            r.setData(patientBanks);
            r.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R selectDrugAllByName(String drugName) {
        R r=new R();
        try {
            List<DrugAll> drugAlls = newVisitsMapper.selectDrugAllListByName(drugName);
            r.setCode(200);
            r.setData(drugAlls);
            r.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R selectInspectionItemByName(String itemName) {
        R r=new R();
        try {
            List<InspectionItem> itemList = newVisitsMapper.selectInspectionItemByName(itemName);
            r.setCode(200);
            r.setData(itemList);
            r.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R selectPreCostListByCostName() {
        R r=new R();
        try {
            List<PrescriptionCost> prescriptionCosts = newVisitsMapper.selectAllPrescriptionCost();
            r.setCode(200);
            r.setData(prescriptionCosts);
            r.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R selectPatientBankRecordsById(Long bankId) {
        R r=new R();
        try {
            PatientBank patientBank = newVisitsMapper.selectPatientBankRecordsById(bankId);
            r.setCode(200);
            r.setData(patientBank);
            r.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R selectPatientBankRecordsByRegId(Long id) {
        R r=new R();
        try {
            Long pbId = newVisitsMapper.selectPatientBankIdByRegId(id);
            PatientBank patientBank = newVisitsMapper.selectPatientBankRecordsById(pbId);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(patientBank);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R selectAllSeeDoctor() {
        R r=new R();
        try {
            List<SeeDoctor> seeDoctors = newVisitsMapper.selectAllSeeDoctor();
            for (SeeDoctor s:seeDoctors) {
                PatientRecords patientRecords = newVisitsMapper.selectPatientRecordsById(s.getRecordsId());
                PatientBank patientBank = newVisitsMapper.selectPatientBankById(patientRecords.getPersonalRecordsId());
                s.setPatientBankRef(patientBank);
            }
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(seeDoctors);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }
}
